package com.exercise.AndroidCamera;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.telpoo.frame.model.BaseModel;
import com.telpoo.frame.model.BaseTask;
import com.telpoo.frame.model.TaskParams;
import com.telpoo.frame.net.BaseNetSupportBeta;
import com.telpoo.frame.object.BaseObject;
import com.telpoo.frame.utils.JsonSupport;

public class Task extends BaseTask implements TaskType{

	public Task(BaseModel baseModel, int taskType, ArrayList<?> list,
			Context context) {
		super(baseModel, taskType, list, context);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected Boolean doInBackground(TaskParams... params) {
		
		switch (taskType) {
		case TASK_RECOGNITION:
			
			String path=(String) dataFromModel.get(0);
			String urlImg = MyUtils.uploadImgur(path, null);
			String url="http://rekognition.com/func/api/?api_key=1234&api_secret=5678&jobs=face_part_detail_recognize_emotion_beauty_gender_emotion_race_eye_smile_mouth_age_aggressive&name_space=test_example&user_id=test_example&urls=";
			url=url+urlImg;
			
			String res= BaseNetSupportBeta.getInstance().method_GET(url);
			if(res==null){
				msg="no network avaiable";
				return TASK_FAILED;
			}
			
			try {
				JSONObject joj=new JSONObject(res);
				
				JSONArray face_detection=joj.getJSONArray("face_detection");
				JSONObject joj1= face_detection.getJSONObject(0);
				//JSONObject emotion= joj1.getJSONObject("emotion");
				BaseObject oj1=new BaseObject();
				
				
				//oj.set("happy", emotion.getString("happy"));
				//oj.set("confused", emotion.getString("confused"));
				//oj.set("surprised", emotion.getString("surprised"));
				
//				oj.set("age", joj1.getString("age"));
//				oj.set("smile", joj1.getString("smile"));
//				oj.set("glasses", joj1.getString("glasses"));
//				oj.set("sunglasses", joj1.getString("sunglasses"));
//				oj.set("eye_closed", joj1.getString("eye_closed"));
//				oj.set("mouth_open_wide", joj1.getString("mouth_open_wide"));
//				oj.set("beauty", joj1.getString("beauty"));
//				oj.set("sex", joj1.getString("sex"));
				
				
				oj1= JsonSupport.jsonObject2BaseOj(joj1);
				BaseObject oj = int2Value(oj1);
				
				ArrayList<BaseObject> ojs=new ArrayList<BaseObject>();
				ojs.add(oj);
				dataReturn=ojs;
				return TASK_DONE;
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			msg="Cannot detect face";
			return TASK_FAILED;
			
			

		default:
			break;
		}
		
		
		return super.doInBackground(params);
	}

	private BaseObject int2Value(BaseObject oj1) {
		
		BaseObject oj = new BaseObject();
		oj.set("age", ""+oj1.get("age"));
		
		if(0.5<oj.getFloat("sex")&&oj.getFloat("sex")!=Float.MIN_VALUE)
			oj.set("sex", "Male");
		else oj.set("sex", "Female");
		
		if(0.5<oj.getFloat("glasses")&&oj.getFloat("glasses")!=Float.MIN_VALUE)
			oj.set("glasses", "Yes");
		else oj.set("glasses", "No");
		
		if(0.5<oj.getFloat("mouth_open_wide")&&oj.getFloat("mouth_open_wide")!=Float.MIN_VALUE)
			oj.set("mouth_open_wide", "Yes");
		else oj.set("mouth_open_wide", "No");
		
		if(0.5<oj.getFloat("beauty")&&oj.getFloat("beauty")!=Float.MIN_VALUE)
			oj.set("beauty", "Beautiful");
		else oj.set("beauty", "Nomal	");
		
		return oj;
			

	}
	

}
